package com.springcloud_consumer.controller;

import com.springcloud_api.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * ClassName:DeptConsumerController
 * Package:com.springcloud_consumer.controller
 * Description
 *
 * @Author:@wenxueshi
 * @Create:2023/2/5 - 15:42
 * @Version:v1.0
 */
@RestController
@RequestMapping("/consumer")
public class DeptConsumerController {
    //注意：消费者不应该有service层
    //RestTemplate,注册到Spring中
    //String url, Class<T> responseType
    @Autowired//提供多种便捷访问远程http服务的方法，简单的restful模板
    private RestTemplate restTemplate;
    //不用ribbon负载均衡，使用端口号去访问
    //private static final String REST_URL_PREFIX = "http://localhost:8001";

    //使用ribbon负载均衡，使用提供者服务名来访问
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/dept/{id}")
    public Dept get(@PathVariable Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/"+id,Dept.class);
    }

    @RequestMapping("/dept/add")
    public boolean addDept(Dept dept){//不能加@requestBody此注解
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add",dept,boolean.class);
    }

    @RequestMapping("/list")
    public List<Dept> getAllDept(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list",List.class);
    }
}
